// Package vipers
/*
@Coding : utf-8
@Time : 2023/3/11 16:29
@Author : yizhigopher
@Software : GoLand
*/
package vipers

import (
	"fmt"
	"ginchat/utils/errorx"
	"ginchat/utils/message"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
	"sync"
)

var (
	rpcViper *viper.Viper
	rpcOnce  sync.Once
)

func GetRPCViper() *viper.Viper {
	rpcOnce.Do(func() {
		rpcViper = viper.New()
		rpcViper.SetConfigType("yaml")
		rpcViper.AddConfigPath("./configs")
		rpcViper.SetConfigName("rpc")

		if err := rpcViper.ReadInConfig(); err != nil {
			errorx.ErrorExit(err)
		}

		rpcViper.OnConfigChange(func(in fsnotify.Event) {
			msg := fmt.Sprintf("%s do %s", in.Name, in.Op)
			message.Println("rpc config file has been changed. ", msg)
		})
	})
	return rpcViper
}
